package org.nobject.common.text;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.nobject.common.lang.StringUtils;

/**
 * Tag
 *
 * @author bianrongjun
 * @version 1.0
 */
public class Tag{
	
	/** 标签名称 */
	private String tagName="";
	/** 属性 */
	private Map attributes=new LinkedHashMap();
	/** 自标签 */
	private List childTags=new LinkedList();
	/**  标签体 */
	public String bodyText=null;
	/** 占空符 */
	public String spacer=" ";
	
	/** 父标签 */
	private Tag parentTag;
	
	/**
	 * 构造函数 
	 * @param parentTag
	 * @param tagName
	 * @param bodyText
	 */
	public Tag(Tag parentTag,String tagName,String bodyText){
		if(parentTag!=null){
			this.parentTag=parentTag;
			this.parentTag.addChildTag(this);
		}
		this.tagName=tagName;
		this.bodyText=bodyText;
	}
	
	/**
	 * 构造函数 
	 * @param parentTag
	 * @param tagName
	 */
	public Tag(Tag parentTag,String tagName){
		this(parentTag, tagName, null);
	}
	
	/**
	 * 构造函数 
	 * @param tagName
	 */
	public Tag(String tagName){
		this(null, tagName, null);
	}
	
	/**
	 * 构造函数 
	 * @param tagName
	 * @param bodyText
	 */
	public Tag(String tagName,String bodyText){
		this(null, tagName, bodyText);
	}
	
	/** 设置属性 */
	public Tag setAttribute(String attributeName,String attributeValue){this.attributes.put(attributeName, attributeValue);return this;}
	/** 设置属性 */
	public Tag setAttribute(String attributeName,int attributeValue){return setAttribute(attributeName,String.valueOf(attributeValue));}
	/** 设置属性 */
	public Tag setAttribute(String attributeName,long attributeValue){return setAttribute(attributeName,String.valueOf(attributeValue));}
	/** 设置属性 */
	public Tag setAttribute(String attributeName,boolean attributeValue){return setAttribute(attributeName,String.valueOf(attributeValue));}
	/** 设置属性 */
	public Tag setAttribute(String attributeName,float attributeValue){return setAttribute(attributeName,String.valueOf(attributeValue));}
	/** 设置属性 */
	public Tag setAttribute(String attributeName,double attributeValue){return setAttribute(attributeName,String.valueOf(attributeValue));}
	/** 获取属性 */
	public String getAttribute(String attributeName){return (String)this.attributes.get(attributeName);}
	/** 获取父标签 */
	public Tag getParentTag() { return parentTag; }
	/** 设置父标签 */
	public Tag setParentTag(Tag parentTag) {this.parentTag = parentTag;return this;}
	/** 添加子标签 */
	public Tag addChildTag(Tag childTag){this.childTags.add(childTag);childTag.setParentTag(parentTag);return this;}
	/** 获取标签名称 */
	public String getTagName() {return tagName;}
	/** 设置标签名称 */
	public Tag setTagName(String tagName) {this.tagName = tagName;return this;}
	/** 获取属性组 */
	public Map getAttributes() {return attributes;}
	/** 获取子标签组 */
	public List getChildTags() {return childTags;}
	/** 获取标签体 */
	public String getBodyText() {return bodyText;}
	/** 设置标签体 */
	public Tag setBodyText(String bodyText) {this.bodyText = bodyText;return this;}

	/** 获取XML */
	public String getXml(){
		StringBuffer sb=new StringBuffer();
		sb.append("<");
		sb.append(this.tagName);
		if(this.attributes.keySet().size()>0){
			for (Iterator iterator = this.attributes.keySet().iterator(); iterator.hasNext();) {
				String attributeName = (String)iterator.next();
				String attributeValue = StringUtils.null2Empty((String)(this.attributes.get(attributeName)));
				sb.append(spacer).append(attributeName).append("=").append("\"").append(attributeValue).append("\"");
			}
		}
		sb.append(">");
		
		if(this.childTags.size()>0){
			for (int i = 0; i < this.childTags.size(); i++) {
				Tag childTag = (Tag) this.childTags.get(i);
				sb.append(childTag.getXml());
			}
		}else if(!StringUtils.isEmpty(this.bodyText)){
			sb.append(StringUtils.null2Empty(this.bodyText));
		}
		sb.append("</"+this.tagName+">");
		return sb.toString();
	}
	
	public static void main(String[] args) {
		Tag tagHTML=new Tag("html");
		Tag tagHead=new Tag(tagHTML,"head");tagHead.setBodyText("THIS IS A TEST");
		Tag tagBody=new Tag(tagHTML,"body");tagBody.setAttribute("bgColor","white");
		System.err.println(tagHTML.getXml());
	}
	
}
